#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct{
     int x;
     int y;
     int width;   
     int height;
}GOZ;

int main(){
    IplImage * resim;
    CvScalar s,s1,gr;
    int i,j,Y,Cb,Cr,t,dil,er,gs;
   CvMemStorage* storage = cvCreateMemStorage(0);
   CvSeq* contour = 0;
   GOZ g[5],l,r;

  // CvCapture *input;
  //  input =  cvCaptureFromCAM(0);
    resim = cvLoadImage("crop.jpg",1);
    IplImage* ycb = cvCreateImage(cvSize(resim->width,resim->height), IPL_DEPTH_8U, 3);
    IplImage* son2 = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 1);
    IplImage* eyemapc = cvCreateImage(cvSize(resim->width,resim->height), IPL_DEPTH_8U, 1);

    cvNamedWindow( "My Window", 1 );
  //  cvNamedWindow( "My Window2", 1 );
 //   IplImage* eyemapl = cvCreateImage(cvSize(resim->width,resim->height), IPL_DEPTH_8U, 1);
 //   IplImage* eyemapc = cvCreateImage(cvSize(resim->width,resim->height), IPL_DEPTH_8U, 1);
// for(;;){
 //   resim = cvQueryFrame(input);
    cvCvtColor(resim,ycb,CV_BGR2YCrCb);    
   
    CvMat *cb = cvCreateMat( resim->height, resim->width, CV_32FC1);
    CvMat *cr = cvCreateMat( resim->height, resim->width, CV_32FC1);
    CvMat *cbcr = cvCreateMat( resim->height, resim->width, CV_32FC1);
    CvMat *gri = cvCreateMat( resim->height, resim->width, CV_32FC1);
                       
    //cvErode(src,dst,element,1);
//    cvDilate(yeni,yeni,element,1); 

    for(i=0;i<(ycb->height)-1;i++){
      for(j=0;j<(ycb->width)-1;j++){
        s=cvGet2D(ycb,i,j);
     //   gr.val[0]=s.val[0];
     //   cvSet2D(gri,i,j,gr);
        cvmSet(gri,i,j,s.val[0]);
        cvmSet(cb,i,j,pow((int)s.val[2],2));
        cvmSet(cr,i,j,pow((int)(255-s.val[1]),2));
        cvmSet(cbcr,i,j,(float)(s.val[2]/s.val[1]));
      }
    }
    
    cvNormalize(cb,cb,0,255, CV_MINMAX, 0);
    cvNormalize(cr,cr,0,255, CV_MINMAX, 0);
    cvNormalize(cbcr,cbcr,0,255, CV_MINMAX, 0);
    
    for(i=0;i<(ycb->height);i++){
      for(j=0;j<(ycb->width);j++){
      s1.val[0]=(cvmGet(cb,i,j)+cvmGet(cr,i,j)+cvmGet(cbcr,i,j))/3;
      cvSet2D(eyemapc,i,j,s1);
      }
    }
   cvEqualizeHist( eyemapc,eyemapc );
   
   CvRect bndRect = cvRect(0,0,0,0);
   CvPoint pt1, pt2,pt3,pt4;

       cvSaveImage("eyemapc1.jpg",eyemapc);
   
   cvThreshold(eyemapc, eyemapc, 150, 255, CV_THRESH_BINARY);
   cvSaveImage("eyemapc.jpg",eyemapc);
   i=0;
   cvFindContours( eyemapc, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
       for( ; contour != 0; contour = contour->h_next ){
   bndRect = cvBoundingRect(contour, 0);
   pt1.x = bndRect.x;
   pt1.y = bndRect.y;
   pt2.x = bndRect.x + bndRect.width;
   pt2.y = bndRect.y + bndRect.height;
   
   if(pt1.x>5 && pt2.x<resim->width-5 && pt1.y<resim->height/2 && pt1.y>10 && (bndRect.width*bndRect.height)>80){
   if(i<5){
   g[i].x=pt1.x;
   g[i].y=pt1.y;
   g[i].width=bndRect.width;
   g[i].height=bndRect.height;
   printf("%d %d\n",g[i].width,g[i].height); 
   i++;
 //  cvRectangle(resim, pt1, pt2, CV_RGB(0,255,0), 1);
   printf("%d %d %d %d\n",pt1.x,pt1.y,pt2.x,pt2.y);
   }
   }
}
printf("kare sayisi:%d\n",i);
gs=i;
  if(i==0){
   printf("Goz Yok!\n");
  }else{
     while(i>0){
       i=i-1;
       if(g[i].x<=resim->width/1.5 && g[i].x<r.x){
         r=g[i];
          if(gs==1){
            l.width=-1;
          }
      /* }else if(g[i].x>resim->width/2 && r.){
         l=g[i];    
          if(gs==1){
          l.width=-1;
          }
       }*/
      }
     }
      i=gs; 
      if(l.width!=-1){
       while(i>0){
        i=i-1;
        if(g[i].x>r.x && abs(r.y-g[i].y)<15 && (abs(r.x-g[i].x)<resim->width*0.6)){
         l=g[i];
         }
       }
      }
  }
  if(l.width>resim->width){
  l.width=-1;
  }
  if(r.width>resim->width){
  r.width=-1;
  }
  IplImage* dst;
  if(l.width!=-1 && r.width!=-1){
  dst=cvCreateImage(cvSize(resim->width*((float)20/(l.x-(r.x+r.width))),resim->height*((float)20/(l.x-(r.x+r.width)))), IPL_DEPTH_8U, 3);
  cvResize(resim,dst);
  }
  printf("right:%d %d\nleft:%d %d\n",r.width,r.height,l.width,l.height);
pt3.x=r.x;
pt3.y=r.y;
pt4.x=r.x+r.width;
pt4.y=r.y+r.height;
cvRectangle(resim, pt3, pt4, CV_RGB(255,0,0), 1);
pt3.x=l.x;
pt3.y=l.y;
pt4.x=l.x+l.width;
pt4.y=l.y+l.height;
cvRectangle(resim, pt3, pt4, CV_RGB(0,0,255), 1);
cvSaveImage("goz.jpg",dst);
    cvShowImage("My Window",resim);
    cvWaitKey(70000);
    
 //   cvSaveImage("eyemapl.jpg",eyemapl);
//    cvReleaseMat(&cb);
//    cvReleaseMat(&gri);
 //   cvReleaseMat(&cr);
 //   cvReleaseMat(&cbcr);
  cvReleaseImage(&resim);
  cvReleaseImage(&dst);
 cvReleaseImage(&ycb);
 cvReleaseImage(&son2);
  cvReleaseImage(&eyemapc);
 cvDestroyWindow("My Window");
// cvDestroyWindow("My Window2");
//  cvReleaseCapture(&input);
    system("pause");
    return 0;
}
